<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>系统设置</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="{{resource}}/mini/lib/layui-v2.5.5/css/layui.css" media="all">

    <script src="https://cdn.bootcss.com/ace/1.4.8/ace.js"></script>
    <script src="https://cdn.bootcss.com/ace/1.4.8/ext-beautify.js"></script>
    <script src="https://cdn.bootcss.com/ace/1.4.8/ext-language_tools.js"></script>
    <style>
        #editor {
            position: absolute;
            width: 100%;
            height: 680px;
        }
    </style>
</head>

<body style="background-color: #dddddd;">
    <br>
    <div class="layui-container">
        <div class="layui-row">
            <div class="layui-col-md1">&nbsp;</div>
            <div class="layui-col-md10">
                <blockquote class="layui-elem-quote" style="color: red;">
                    提示信息：配置前请先前往官方网站查看，擅自配置可能导致系统崩溃！！
                </blockquote>

                <div id="editor" style="display:none;">{{context}}</div><br>

                <!-- 占位DIV -->
                <div style="height:680px;"></div>
                <!-- 占位DIV -->

                <button class="layui-btn" lay-submit lay-filter="setting">保存配置</button>


            </div>
            <div class="layui-col-md1">&nbsp;</div>
        </div>
    </div>

    <script src="{{resource}}/mini/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
    <script>
        layui.use(['form', 'element'], function () {
            var form = layui.form,
                $ = layui.jquery,
                element = layui.element,
                layer = layui.layer;

            var editor = ace.edit('editor');
            editor.setTheme('ace/theme/clouds');
            editor.session.setMode('ace/mode/json');

            editor.setFontSize(18);

            editor.commands.addCommand({
                name: 'saveFile',
                bindKey: {
                    win: 'Ctrl-S',
                    mac: 'Command-S'
                },
                exec: save_file,
                readOnly: false
            });


            // 页面渲染完毕后再显示
            $('#editor').css('display', 'block');

            //监听提交
            form.on('submit(setting)', function (data) {
                save_file();
            });

            function save_file() {

                // 数据列表
                var context = editor.getValue();

                try {
                    var obj = JSON.parse(context);
                    if (!(typeof obj == 'object' && obj)) {
                        layer.msg('保存错误：内容不符合格式要求！');
                        return false;
                    }

                } catch (e) {
                    layer.msg('保存错误：内容不符合格式要求！');
                    return false;
                }

                layer.prompt({
                    title: '本操作需要验证您的登录密码：'
                }, function (pass, index) {
                    layer.close(index);
                    $.post('{{domain}}/admin/api/settings?which=develop&do=update', {
                        context: context,
                        password: pass
                    }, function (res) {
                        if (res.data == 'OK') {
                            layer.msg('设置更新成功!');
                        } else {
                            layer.msg(res.msg + '...');
                        }
                    }).fail(function (res) {
                        layer.msg('操作异常：' + res.responseJSON.msg + '！');
                    });
                });
            }

        });
    </script>
</body>

</html>